import 'reflect-metadata';
type MyPropDecorator = (targetClassPrototype: any,propertyKey: string | symbol) => void;

export  function Singleton(isSingleton?: boolean): MyPropDecorator {
  return (targetClassPrototype, propertyKey) => {
    let PropServiceClass:any =Reflect.getMetadata("design:type",targetClassPrototype,propertyKey)
    let ServiceImplClass: any = PropServiceClass.getServiceImplClass()
    let ServiceImplInstanceOrClass

    let metaSingleton = Reflect.getMetadata('singleton', targetClassPrototype, propertyKey);
    if (isSingleton) {
      //  是单例设计模式
      if (!metaSingleton) {
        Reflect.defineMetadata( 'singleton', isSingleton, targetClassPrototype, propertyKey);
        ServiceImplInstanceOrClass = ServiceImplClass.getInstance();
      } else {
        console.log("单件模式创建，使用了上一次的业务对象")
      }
    } else {
      ServiceImplInstanceOrClass = ServiceImplClass;
    }
    Reflect.defineMetadata( 'ServiceImplInstanceOrClass',  ServiceImplInstanceOrClass, targetClassPrototype,propertyKey);

  };
}
